import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import contestShowView from '@/views/main/contestShowView.vue'
import infoView from '@/views/main/infoView.vue'
import rankView from '@/views/main/rankView.vue'
import myBotsView from '@/views/main/myBotsView.vue'
import loginView from '@/views/user/loginView.vue'
import registerView from '@/views/user/registerView.vue'
import notfoundView from '@/views/main/notfoundView.vue'
import store from '@/store'
import pkView from '@/views/pk/pkView.vue'
import intrView from '@/views/main/intrView.vue'
const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      login: false,
      name: "RobotField",
      base:false,
    }
  },
  {
    path: '/intr/',
    name: 'intr',
    component: intrView,
    meta: {
      login: false,
      name: "简介",
      base:true,
    }
  },
  {
    path: '/pk/',
    name: 'pk',
    component: pkView,
    meta: {
      login: true,
      name: "对战",
      base:true,
    }
  },
  {
    path: '/contestShow/',
    name: 'contestShow',
    component: contestShowView,
    meta: {
      login: false,
      name: "对局列表",
      base:true,
    }
  },
  {
    path: '/infoShow/',
    name: 'infoShow',
    component: infoView,
    meta: {
      login: true,
      name: "个人信息",
      base:true,
    }
  },
  {
    path: '/rankShow/',
    name: 'rankShow',
    component: rankView,
    meta: {
      login: false,
      name: "天梯排行",
      base:true,
    }
  },
  {
    path: '/myBots/',
    name: 'myBots',
    component: myBotsView,
    meta: {
      login: true,
      base:true,
    }
  },
  {
    path: '/login/',
    name: 'login',
    component: loginView,
    meta: {
      login: false,
      name: "登录",
      base:true,
    }
  },
  {
    path: '/register/',
    name: 'register',
    component: registerView,
    meta: {
      login: false,
      name: "注册",
      base:true,
    }
  },
  {
    path: '/404/',
    name: 'notfound',
    component: notfoundView,
    meta: {
      login: false,
      name: "404",
      base:true,
    }
  },
  {
    path: '/:pathMatch(.*)',
    redirect: "/404/",
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

export default router
router.beforeEach((to, from, next) => {
  window.scroll(0, 0);//回到顶部
  document.title = to.meta.name;
  if (to.meta.login && !store.state.User.isLogin) next({ name: "login" });
  else next();
})